package com.ulic.customerManApplication.achievementmodule.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ulic.customerManApplication.achievementmodule.bo.Achievement;
import com.ulic.customerManApplication.achievementmodule.service.AchievementService;
import com.ulic.customerManApplication.achievementmodule.vo.AchievementVO;
import com.ulic.customerManApplication.base.BaseAction;
import com.ulic.customerManApplication.commonmodule.bo.Organization;
import com.ulic.customerManApplication.constant.CONSTANT;
import com.ulic.customerManApplication.pager.PagerModel;
import com.ulic.customerManApplication.usermodule.bo.User;
import com.ulic.customerManApplication.util.excel.DownloadExcel;
import com.ulic.customerManApplication.util.excel.ExpAchievements;

@Component("achievementAction")
@Scope("prototype")
public class AchievementAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Achievement achievement;
	private AchievementService achievementService;
	private String startcreate;
	private String endcreate;
    private String subCityHidden;
	
	public String getSubCityHidden() {
		return subCityHidden;
	}
	public void setSubCityHidden(String subCityHidden) {
		this.subCityHidden = subCityHidden;
	}

	public String getStartcreate() {
		return startcreate;
	}

	public void setStartcreate(String startcreate) {
		this.startcreate = startcreate;
	}

	public String getEndcreate() {
		return endcreate;
	}

	public void setEndcreate(String endcreate) {
		this.endcreate = endcreate;
	}

	public AchievementService getAchievementService() {
		return achievementService;
	}

	@Resource
	public void setAchievementService(AchievementService achievementService) {
		this.achievementService = achievementService;
	}

	public Achievement getAchievement() {
		return achievement;
	}

	public void setAchievement(Achievement achievement) {
		this.achievement = achievement;
	}

	public String searchAchievements() {

		HttpServletRequest httpServletRequest = ServletActionContext
				.getRequest();
		String buttonName = httpServletRequest.getParameter("buttonType");
		if(!"exp".equals(buttonName)){
		
		String roleName = (String) session.get("rolename");
		String startDate  = httpServletRequest.getParameter("startcreate");
		String endDate  = httpServletRequest.getParameter("endcreate");
		User user = (User) session.get(CONSTANT.USER_SESSION_KEY);
		AchievementVO achievementVO = new AchievementVO();
		achievementVO.setRoleName(roleName);
		if(achievement==null){
			achievement = new Achievement();
		}
		if("SUBADMIN".equals(roleName)){
			 Organization organ = (Organization)session.get("barchList");
			 if(organ!=null){
				 achievement.setSubCompany(organ.getUm_organ_id());
			 }
		}else if("THIRDADMIN".equals(roleName)){
			Organization subComOrgan = (Organization)session.get("barchList");
			 if(subComOrgan!=null){
				 achievement.setSubCompany(subComOrgan.getUm_organ_id());
			 }
			Organization organ = (Organization)session.get("expensedList");
			 if(organ!=null){
				 achievement.setSubCity(organ.getUm_organ_id());
			 }
		} 
		achievementVO.setStartcreate(startDate);
		achievementVO.setEndcreate(endDate);
		achievementVO.setAchievement(achievement);
		achievementVO.setUser(user);
		PagerModel pm = achievementService.getAchievements(achievementVO,
				curPage);
		httpServletRequest.setAttribute("pm", pm);
		httpServletRequest.setAttribute("roleName", roleName);
		return SUCCESS;
		}else{
			return expAchievements();
		}
	}

	public String viewAchievement() throws Exception {
		achievement = achievementService.getAchievement(achievement.getId());
		return SUCCESS;
	}

	public String updateAchievement() throws Exception {
		achievement = achievementService.getAchievement(achievement.getId());
		return SUCCESS;
	}

	public String updatedAchievement() throws Exception {
		User user = (User) session.get(CONSTANT.USER_SESSION_KEY);
		achievement.setUser(user);
		achievementService.updateAchievement(achievement);
		return SUCCESS;
	}

	public String deleteAchievement() throws Exception {
		achievement = achievementService.getAchievement(achievement.getId());
	    achievementService.deleteAchievement(achievement);
		return SUCCESS;
	}

	public String expAchievements() {
		HttpServletRequest httpServletRequest = ServletActionContext
				.getRequest();
		
		
			
		HttpServletResponse httpServletResponse = ServletActionContext
				.getResponse();
		User user = (User) session.get(CONSTANT.USER_SESSION_KEY);
		String realPath = httpServletRequest.getRealPath("/download/");
		String fileName = "成果信息.xls";
		String[] headers = { "分公司", "中支", "名称", "数量", "学校" };

		String roleName = (String) session.get("rolename");
		String startDate  = httpServletRequest.getParameter("startcreate");
		String endDate  = httpServletRequest.getParameter("endcreate");
		AchievementVO achievementVO = new AchievementVO();
		achievementVO.setRoleName(roleName);
		if(achievement==null){
			achievement = new Achievement();
		}
		if("SUBADMIN".equals(roleName)){
			 Organization organ = (Organization)session.get("barchList");
			 if(organ!=null){
				 achievement.setSubCompany(organ.getUm_organ_id());
			 }
		}else if("THIRDADMIN".equals(roleName)){
			Organization subComOrgan = (Organization)session.get("barchList");
			 if(subComOrgan!=null){
				 achievement.setSubCompany(subComOrgan.getUm_organ_id());
			 }
			Organization organ = (Organization)session.get("expensedList");
			 if(organ!=null){
				 achievement.setSubCity(organ.getUm_organ_id());
			 }
		} 
		achievementVO.setStartcreate(startDate);
		achievementVO.setEndcreate(endDate);
		achievementVO.setAchievement(achievement);
		achievementVO.setUser(user);
		PagerModel pm = achievementService.getAchievements(achievementVO,
				curPage);
		ExpAchievements.exp(headers, achievementService.getAchievements(achievementVO,1,pm.getTotal()==0?1:pm.getTotal()).getDatas(), realPath + "/" + fileName);// 将数据读取到服务器上的文件中
		DownloadExcel.download(realPath, fileName, httpServletRequest,
				httpServletResponse);// 从服务器上下载文件
		return null;
	}
}
